package com.person.jc.web.car;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.Constants;
import com.person.jc.entity.TsUser;
import com.person.jc.service.car.BdCarServiceInfoService;
import com.person.jc.utils.DateUtils;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.Utils;

public class BdCarServiceInfoController extends Controller {

	public void init() {
		setAttr("menuId", getPara("menuId"));
		render("main.jsp");
	}

	public void list() {
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		TsUser tsUser = getSessionAttr(Constants.LOGIN_USER);
		map.put("operUser", tsUser.getUserId());
		List<Record> list = BdCarServiceInfoService.me.getList(map);
		setAttr("list", list);
		render("list.jsp");
	}

	public void edit() {
		setAttr("now", DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
		String serviceId = getPara("serviceId");
		String view = getPara("view");
		if (Utils.isNotEmpty(serviceId)) {
			Map<String, Object> map = new HashMap<String, Object>();
			TsUser tsUser = getSessionAttr(Constants.LOGIN_USER);
			map.put("operUser", tsUser.getUserId());
			map.put("serviceId", serviceId);
			List<Record> list = BdCarServiceInfoService.me.getList(map);
			if (list.size() != 0) {
				setAttr("obj", list.get(0));
			}
			setAttr("view", view);
		}
		render("edit.jsp");
	}

	public void save() {
		String res = "success";
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		String serviceId = String.valueOf(map.get("serviceId"));
		try {
			TsUser tsUser = getSessionAttr(Constants.LOGIN_USER);
			map.put("operUser", tsUser.getUserId());
			if(Utils.isNotEmpty(serviceId)){
				BdCarServiceInfoService.me.update(map);
			}else{
				BdCarServiceInfoService.me.save(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			res = "error";
		}
		renderText(res);
		
	}

	public void delete() {
		String res = "success";
		String serviceIds = getPara("serviceIds");
		try {
			for (String id : serviceIds.split(",")) {
				BdCarServiceInfoService.me.delete(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			res = "error";
		}
		renderText(res);
	}
}
